package com.kanche.bigbaby.collection;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kanche.bigbaby.R;
import com.kanche.bigbaby.collection.domain.Photo;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.squareup.picasso.Picasso;

import java.util.List;

//import com.kkche.bss.R;
//import com.kkche.bss.domain.Photo;

/**
 * @author: nealmi
 * Date: 9/27/13
 * Time: 5:12 PM
 */
public class VehiclePhotosEditorAdapter extends BaseAdapter {
    private List<Photo> mList;
    private Context mContext;

    public VehiclePhotosEditorAdapter(List<Photo> list, Context context) {
        mList = list;
        mContext = context;
    }

    public void refresh(List<Photo> list) {
        mList = list;
        notifyDataSetChanged();
    }

    public void update(String key, String uri) {
        for (Photo photo : mList) {
            if (key.equalsIgnoreCase(photo.getKey())) {
                photo.setUri(uri);
                notifyDataSetChanged();
                break;
            }
        }
    }

    public List<Photo> getData() {
        return mList;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        View root = null;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            root = inflater.inflate(R.layout.photos_editor_item, null);
            holder = new Holder();
            holder.imageView = (ImageView) root.findViewById(R.id.imageView);
            holder.labelTxt = (TextView) root.findViewById(R.id.txt_label);
            holder.cover = (TextView) root.findViewById(R.id.cover);
            root.setTag(holder);
        } else {
            root = convertView;
            holder = (Holder) root.getTag();
        }
        if (position == Photo.COVER_INDEX) {
            holder.cover.setVisibility(View.VISIBLE);
        } else {
            holder.cover.setVisibility(View.GONE);
        }
        Photo photo = (Photo) getItem(position);
        if ("add_photo".equalsIgnoreCase(photo.getKey())) {
            Picasso.with(mContext).load(R.drawable.add_photo_btn)
                    .resizeDimen(R.dimen.image_thum_width, R.dimen.image_thum_height)
                    .centerCrop()
                    .into(holder.imageView);
        } else {
            String path = photo.getUri();

            if (StringUtils.hasText(path)) {
                DisplayImageOptions options = new DisplayImageOptions.Builder()
//                        .considerExifParams(MerchantService.CurrentAppConfig.CONSIDER_EXIF_PARAMS)
                        .considerExifParams(true)
                        .showImageOnFail(R.drawable.place_holder_small)
//                        .showImageOnLoading(R.drawable.place_holder_small)
//                        .resetViewBeforeLoading(true)
                        .build();
                if (StringUtils.isHttpPath(path)) {
//                    path += "@160w_120h_90q";
                    path = StringUtils.transformImageCdnUrl(path, 160, 120, 90);

                    ImageLoader.getInstance().displayImage(path, holder.imageView, options);
//                    Picasso.with(mContext).load(path)
//                            .placeholder(R.drawable.place_holder_small)
//                            .resizeDimen(R.dimen.image_thum_width, R.dimen.image_thum_height)
//                            .centerCrop()
//                            .into(holder.imageView);
                } else {
                    ImageLoader.getInstance().displayImage("file:/"+path, holder.imageView, options);

//                    Picasso.with(mContext).load(new File(path))
//                            .resizeDimen(R.dimen.image_thum_width, R.dimen.image_thum_height)
//                            .into(holder.imageView);
                }
            } else {
                Drawable drawable = mContext.getResources().getDrawable(photo.getPlaceHolderResourceId());
                drawable.setAlpha(50);
//            drawable.setColorFilter(R.color.black_translucent, PorterDuff.Mode.LIGHTEN);
//            holder.imageView.setImageDrawable(drawable);
            }
            holder.labelTxt.setText(photo.getLabel());
            holder.labelTxt.setVisibility(View.VISIBLE);

        }

        return root;
    }

//    public void add(String target) {
//        String key = "photo_" + mList.size();
//        mList.add(new Photo(key, target));
////        for (Photo photo : mList) {
////            if (!StringUtil.hasText(photo.getUri())) {
////                photo.setUri(target);
////                break;
////            }
////        }
//    }

    public void add(String target, String original) {
        String key = "photo_" + mList.size();
        Photo photo = new Photo(key, target);
        photo.setOriginal(original);

        mList.add(photo);
    }

    public void commit() {
        notifyDataSetChanged();
    }

    public void remove(Photo photo) {
        mList.remove(photo);
    }

    public void moveToFirst(Photo photo) {
        mList.remove(photo);
        mList.add(Photo.COVER_INDEX, photo);
    }

    class Holder {
        public TextView cover;
        private ImageView imageView;
        private TextView labelTxt;
    }
}